package cn.ybk.org.controller;

import cn.ybk.aop.Anno;
import cn.ybk.basic.annotation.PreAuthorize;
import cn.ybk.basic.jwt.UserInfo;
import cn.ybk.basic.util.AjaxResult;
import cn.ybk.basic.util.LoginContext;
import cn.ybk.basic.util.PageList;
import cn.ybk.org.domain.Department;
import cn.ybk.org.domain.Employee;
import cn.ybk.org.query.DepartmentQuery;
import cn.ybk.org.service.DepartmentService;
import cn.ybk.user.domain.Logininfo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController // 相当于 @Controller+@ResponseBody 每个public中都相当于添加上了@ResponBody
@RequestMapping("/dept")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @GetMapping
    public List<Department> findAll() {
        return departmentService.findAll();
    }

    @GetMapping("/{id}")
    public Department findById(@PathVariable("id") Long id) {
        return departmentService.findById(id);
    }

    @Anno
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        Long id = department.getId();
        if (id == null) {
            departmentService.add(department);
        } else {
            departmentService.update(department);
        }
        return AjaxResult.me().success();
    }

    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除部门", sn ="department:delete")
    public AjaxResult delete(@PathVariable Long id) {
        departmentService.delete(id);
        return AjaxResult.me().success();
    }

    @PatchMapping
    public AjaxResult batch(@RequestBody List<Long> ids) {
        departmentService.batch(ids);
        return AjaxResult.me().success();
    }

    @PostMapping
    @PreAuthorize(name = "部门列表", sn = "department:list")
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query) {
        return departmentService.queryByPage(query);
    }

    // 需要查询上级部门，是集合，返回接收为集合
    @GetMapping("/tree")
    @PreAuthorize(name = "部门树木",sn = "department:tree")
    public List<Department> tree() {
        return departmentService.tree();
    }
}
